﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using System.Data.Odbc;

public partial class CreateNewFeedbackUI : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.FindControl("MainMenuAdmin").Visible = true;

        if ((String)Session["Username"] == null || (String)Session["Username"] != "admin")
        {
            //Response.End();
            Response.Redirect("~/LoginAdmin.aspx");
        }
    }

    public Boolean validateEntries()
    {
        int flagAlert = 0;
        String alertMessage = "<Script language=javascript>alert('Please complete the follwing fields : ";
        String fileExtension = Path.GetExtension(xlsUpload.PostedFile.FileName);
        if (courseNameTextbox.Text == "")
        {
            alertMessage += "Course Name, ";
            flagAlert = 1;
        }

        if (courseInstructorTextbox.Text == "")
        {
            alertMessage += "Course Instructor, ";
            flagAlert = 1;
        }

        if (courseInstructorEmailTextbox.Text == "")
        {
            alertMessage += "Course Instructor Email, ";
            flagAlert = 1;
        }

        if (courseCodeTextbox.Text == "")
        {
            alertMessage += "Course Code, ";
            flagAlert = 1;
        }

        if (flagAlert == 1)
        {
            alertMessage += "')</Script>";
            Response.Write(alertMessage);
            return false;
        }

        /*Regex regEmail = new Regex(@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            
        if(regEmail.IsMatch(EmailAddress))*/
       

        else if (xlsUpload.HasFile != true)
        {
            Response.Write("<Script language=javascript>alert('Please upload the xls file')</Script>");
            return false;
        }

        else if (fileExtension != ".xls")
        {
            Response.Write("<Script language=javascript>alert('Please upload Only xls file')</Script>");
            return false;
        }


        else
        {
            flagAlert = 0;
            alertMessage = "<Script language=javascript>alert('Select a valid ";
            if (EndDateCalendar.SelectedDate.Date <= StartingDateCalendar.SelectedDate.Date)
            {
                alertMessage += "- End Date ";
                flagAlert = 1;
            }

            if (SummarizationDateCalendar.SelectedDate.Date <= StartingDateCalendar.SelectedDate.Date || SummarizationDateCalendar.SelectedDate.Date <= EndDateCalendar.SelectedDate.Date)
            {
                alertMessage += "- Summarization Date ";
                flagAlert = 1;
            }

            if (flagAlert == 1)
            {
                alertMessage += "')</Script>";
                Response.Write(alertMessage);
                return false;
            }

            else
                return true;

        }

    }

    protected void initiateNewFeedback(object sender, EventArgs e)
    {
        if (validateEntries())
        {

            DateTime startDate = StartingDateCalendar.SelectedDate;
            DateTime endDate = EndDateCalendar.SelectedDate;
            DateTime summDate = SummarizationDateCalendar.SelectedDate;

            try
            {
                //xlsUpload.PostedFile.SaveAs(Server.MapPath("~/App_Data") + System.IO.Path.DirectorySeparatorChar + xlsUpload.PostedFile.FileName);
                xlsUpload.PostedFile.SaveAs("D://5th Sem//SE//Coding//XLS//" + xlsUpload.PostedFile.FileName);
                FeedbackForm feedback = new FeedbackForm();

                int flag = feedback.checkXLS(xlsUpload.PostedFile.FileName);


                if (flag == 2)
                {
                    Response.Write("<Script language=javascript>alert('Format of xls file is not correct!')</Script>");
                    return;
                }


                else if (flag == 3)
                {
                    Response.Write("<Script language=javascript>alert('xls file is empty!')</Script>");
                    return;
                }

                else if (flag == 4)
                {
                    Response.Write("<Script language=javascript>alert('Some occurred with the xls file. Please try after some time.')</Script>");
                    return;
                }



                feedback.setCourseName(courseNameTextbox.Text);
                feedback.setCourseID(courseCodeTextbox.Text);
                feedback.setDates(startDate, endDate, summDate);
                feedback.setCourseInstructor(courseInstructorTextbox.Text, courseInstructorEmailTextbox.Text);

                List<String> coCourseInstructor = new List<string>();
                List<String> coCourseInstructorEmailID = new List<string>();
                List<String> TA = new List<string>();
                List<String> TAEmailID = new List<string>();

                if (courseCoInstructor1Textbox.Text != "")
                {
                    coCourseInstructor.Add(courseCoInstructor1Textbox.Text);
                    coCourseInstructorEmailID.Add(courseCoInstructor1EmailTextbox.Text);
                }

                if (courseCoInstructor2Textbox.Text != "")
                {
                    coCourseInstructor.Add(courseCoInstructor2Textbox.Text);
                    coCourseInstructorEmailID.Add(courseCoInstructor2EmailTextbox.Text);
                }

                if (courseTA1Textbox.Text != "")
                {
                    TA.Add(courseTA1Textbox.Text);
                    TAEmailID.Add(courseTA1EmailTextbox.Text);
                }

                if (courseTA2Textbox.Text != "")
                {
                    TA.Add(courseTA2Textbox.Text);
                    TAEmailID.Add(courseTA2EmailTextbox.Text);
                }

                if (courseTA3Textbox.Text != "")
                {
                    TA.Add(courseTA3Textbox.Text);
                    TAEmailID.Add(courseTA3EmailTextbox.Text);
                }

                if (courseTA4Textbox.Text != "")
                {
                    TA.Add(courseTA4Textbox.Text);
                    TAEmailID.Add(courseTA4EmailTextbox.Text);
                }

                feedback.setCoCourseInstructor(coCourseInstructor, coCourseInstructorEmailID);
                feedback.setTA(TA, TAEmailID);

                flag = feedback.createNewForm();


                if (flag == 2)
                {
                    Response.Write("<Script language=javascript>alert('Some Problem Occured with the Database!!')</Script>");
                    return;
                }

                else if (flag == 3)
                {
                    Response.Write("<Script language=javascript>alert('Feedback Created but some error occured while sending mails!')</Script>");
                }

                else
                {
                    Response.Write("<Script language=javascript>alert('Feedback Created!!')</Script>");
                }


            }
            catch (Exception ex)
            {
                // error message
                Response.Write("<Script language=javascript>alert('Some problem occurred while creating the feedback. Please try after some time.')</Script>");
                return;

            }
        }

        else
        {
            return;
        }

    }
}
